//
//  DemoBController.swift
//  SwiftBindDemo
//
//  Created by 万孟超 on 2023/12/27.
//

import UIKit
import CSBind

class DemoBController: UIViewController {
    
    @IBOutlet weak var tf: UITextField!

    class Model: NSObject {
        @objc dynamic var a: String? = ""
    }
    
    let model = Model()
    
    var bindDispose: CSBindDispose?
    var index: Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        model.a = "这个是a的原始值"
        // Do any additional setup after loading the view.
    }
    
    @IBAction func bind(_ sender: Any) {
        bindDispose = (model, \.a) ==> (tf, \.text)
        Alert.show("开启单向绑定，TextField的值会随着model的值改变而改变", on: self)
    }
    
    @IBAction func dbind(_ sender: Any) {
        bindDispose = (model, \.a) <==> (tf, \.text)
        Alert.show("开启双向绑定，TextField的值和model的值会相互作用", on: self)
    }
    
    @IBAction func change(_ sender: Any) {
        index += 1
        model.a = "这个是a的改变后的值\(index)"
        showAlert()
    }
    @IBAction func free(_ sender: Any) {
        bindDispose?.free()
    }
    @IBAction func pause(_ sender: Any) {
        bindDispose?.pause()
    }
    @IBAction func restart(_ sender: Any) {
        bindDispose?.run()
    }
    @IBAction func pri(_ sender: Any) {
        showAlert()
    }
    
    func showAlert() {
        let message = "Model.a：\(String(describing: model.a))"
        Alert.show(message, on: self)
    }
}
